﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Entities;
using DLTN.Data;

public partial class Admin_DanhMuc_DMBoSuuTap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind_Data();
        }
    }
    protected void Bind_Data()
    {
        int TotalRows = 0;
        TList<BoSuuTap> TList_Catalog = DataRepository.BoSuuTapProvider.GetPaged("", "Id asc", 0, 0, out TotalRows);
        gridCatalog.DataSource = TList_Catalog;
        gridCatalog.DataBind();
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {//oo
            BoSuuTap entBoSuuTap = new BoSuuTap();
            entBoSuuTap.Id = Convert.ToInt32(H_CatalogId.Value);
            entBoSuuTap.Ten = Common.StandardString(txtTen.Text);
            entBoSuuTap.DienGiai = Common.StandardString(txtDienGiai.Text);
            entBoSuuTap.NgayCapNhat = Convert.ToDateTime(DateTime.Now.ToString());
            if (entBoSuuTap.Id == 0)
            {
                entBoSuuTap.Id = 0;
                updateStatus = DataRepository.BoSuuTapProvider.Insert(entBoSuuTap);
            }
            else
            {
                updateStatus = DataRepository.BoSuuTapProvider.Update(entBoSuuTap);
            }
            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_CAPNHAT_THANHCONG, true);
                Bind_Data();
                cmdReset_Click(sender, e);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMBoSuuTap.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        txtDienGiai.Text = "";
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                BoSuuTap entBoSuuTap = DataRepository.BoSuuTapProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entBoSuuTap != null)
                {
                    txtTen.Text = entBoSuuTap.Ten;
                    txtDienGiai.Text = entBoSuuTap.DienGiai;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMBoSuuTap.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdBoSuuTap = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.BoSuuTapProvider.Delete(IdBoSuuTap);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMBoSuuTap.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BOSUUTAP_XOA_THATBAI, true);
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data();
        txtTen.Text = "";
        txtDienGiai.Text = "";
        H_CatalogId.Value = "0";
    }
    
}